package com.itheima.controller;

import com.itheima.common.dto.MemberDto;
import com.itheima.common.entity.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@RestController("mobileMember")
@RequestMapping("/member")
@Slf4j
@Api(tags = "移动端会员相关接口")
public class MemberController {
    private static final String CODE_KEY_PREFIX = "validate_code_";
    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping("/login")
    public Result login(@RequestBody MemberDto memberDto) {
        log.info("会员登录，手机号：{}", memberDto);
        String telephone = memberDto.getTelephone();
        String code = memberDto.getValidateCode();
        if (Objects.equals(redisTemplate.opsForValue().get(CODE_KEY_PREFIX + telephone), code)) {
            return new Result(true, "登录成功");
        } else {
            return new Result(false, "验证码错误");
        }


    }

}
